package com.yunze.task.yunze.Automation;


import com.alibaba.fastjson.JSON;
import com.yunze.common.mapper.mysql.automationCC.YzAutomationCcHisMapper;
import com.yunze.common.mapper.mysql.automationCC.YzAutomationCcMapper;
import com.yunze.common.mapper.mysql.automationCC.YzAutomationCcUrlMapper;
import com.yunze.common.utils.Email.EmailCc;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

/**
 * 自动同步卡号
 */
@Slf4j
@Component
public class EmaiSynCardInfoMQ {


    @Resource
    private YzAutomationCcHisMapper yzAutomationCcHisMapper;
    @Resource
    private EmailCc emailCc;
    @Resource
    private YzAutomationCcMapper yzAutomationCcMapper;
    @Resource
    private YzAutomationCcUrlMapper yzAutomationCcUrlMapper;


    /**
     * 邮件抄送
     */
    @RabbitHandler
    @RabbitListener(queues = "admin_EmaiSynCardInfo_queue")
    public void ListenerSend(String msg) {
        if (StringUtils.isEmpty(msg)) {
            return;
        }
        Map<String, Object> Pmap = JSON.parseObject(msg);
        Map<String, Object> CcMessage_Map = (Map<String, Object>) Pmap.get("CcMessage_Map");
        String trigger_type = Pmap.get("trigger_type").toString();
        Send(trigger_type, CcMessage_Map);
    }


    public void Send(String trigger_type, Map<String, Object> CcMessage_Map) {

        Map<String, Object> findMap = new HashMap<>();
        findMap.put("trigger_type", trigger_type); //自动化触发类型 13 14
        Map<String, Object> channel_idMap = new HashMap<>();
        List<String> channel_id = new ArrayList<String>();
        channel_id.add("IsNull");
        channel_idMap.put("channel_id", channel_id);
        List<Map<String, Object>> AutomationCcArr = yzAutomationCcMapper.findConcise(findMap);//获取自动化 抄送 组
        if (AutomationCcArr != null && AutomationCcArr.size() > 0) {
            for (int i = 0; i < AutomationCcArr.size(); i++) {
                Map<String, Object> CCObj = AutomationCcArr.get(i);
                List<Map<String, Object>> AutomationCcUrlArr = yzAutomationCcUrlMapper.findConcise(CCObj);//获取自动化 抄送 邮箱
                if (AutomationCcUrlArr != null && AutomationCcUrlArr.size() > 0) {
                    String execution_template = CCObj.get("execution_template").toString();
                    if (execution_template.equals("1")) {
                        for (int j = 0; j < AutomationCcUrlArr.size(); j++) {
                            Object eM = AutomationCcUrlArr.get(j).get("email");
                            if (eM != null && eM.toString().length() > 0) {
                                String SendEmail = eM.toString();
                                Map<String, Object> saveHisMap = new HashMap<>();
                                saveHisMap.put("cc_id", CCObj.get("id"));
                                saveHisMap.put("cc_url", "");
                                saveHisMap.put("cc_email", SendEmail);
                                saveHisMap.put("cc_state", "1");//已抄送
                                saveHisMap.put("cc_parameter", JSON.toJSONString(CcMessage_Map));
                                Map<String, Object> Rmap = null;
                                Boolean bool = false;
                                String remark = "", cc_result = "0";
                                try {
                                    Rmap = emailCc.PerformTasks_default(CcMessage_Map, SendEmail, "系统通知");
                                    bool = (Boolean) Rmap.get("bool");
                                    if (bool) {
                                        cc_result = "1";
                                    }
                                } catch (Exception e) {
                                    remark = e.getMessage();
                                    remark = remark.length() > 240 ? remark.substring(0, 240) : remark;
                                }
                                saveHisMap.put("remark", remark);
                                saveHisMap.put("cc_result", cc_result);
                                boolean saveHis = yzAutomationCcHisMapper.save(saveHisMap) > 0;
                                log.info(">>自动化 [EmaiSynCardInfoMQ] 已抄送邮箱{}  - 插入抄送记录 bool   {} <<", SendEmail, saveHis);
                            } else {
                                log.error(">>自动化 [EmaiSynCardInfoMQ] 抄送邮箱未获取到  - 抄送取消  {} <<", eM);
                            }
                        }
                    } else {
                        log.error(">>自动化 [EmaiSynCardInfoMQ] 抄送模板不批对  - 抄送取消  {} <<");
                    }
                } else {
                    log.error(">>自动化 [EmaiSynCardInfoMQ] 获取自动化 抄送 邮箱 未找到数据 - 抄送取消  {} <<");
                }
            }
        }
    }


}
